﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Data;

namespace ChoJu.Converters.Alarm
{
    public class MessageConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value == null)
                return null;

            var message = value.ToString();
            int count = 60;
            var row = message.Length / count;
            row = row == 0 ? 1 : row;
            StringBuilder sb = new StringBuilder();
            for (int idx = 0; idx < row; idx++)
            {
                if (idx == 3) //对多显示3行
                    break;

                if (idx < 2)
                    sb.AppendLine(message.Substring(idx * count, count > message.Length ? message.Length : count));
                else
                    sb.AppendLine(message.Substring(idx * count, (count > message.Length ? message.Length : count) - 3) + "……");
            }

            return sb.ToString();
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}
